package com.dhcc.core.modules.msgcenter.push;

import java.util.Date;
import java.util.List;

import com.dhcc.core.framework.util.SpringContextHolder;
import com.dhcc.core.modules.msgcenter.constant.MsgReceiveType;
import com.dhcc.core.modules.msgcenter.dao.MsgUserSettingMapper;
import com.dhcc.core.modules.msgcenter.entity.MsgPushLog;
import com.dhcc.core.modules.msgcenter.vo.MsgPush;
import com.dhcc.core.modules.system.entity.User;
/**
 * 推送服务-抽象类
 * @ClassName: AbsPushService 
 * @Description: TODO
 * @author: cyf
 * @date: 2018年11月20日 上午9:40:44
 */
public abstract class AbsPushService {
    /**
     * 推送接口
     * @Title: send 
     * @Description: TODO
     * @param msg
     * @param pushType
     * @return: void
     */
    public abstract void send(MsgPush msgPush,String pushType);
    /**
     * 获取需要推送的用户
     * @Title: getReceiveUsers 
     * @Description: 如果用户没有设置采用默认，如果已设置采用用户的配置
     * @param msg
     * @param pushType
     * @return
     * @return: List<User>
     */
    protected List<User> getReceiveUsers(MsgPush msgPush,String pushType){
        MsgUserSettingMapper usMapper = SpringContextHolder.getBean(MsgUserSettingMapper.class);
        Integer receiveType = msgPush.getReceiveType();
        Long moduleId = msgPush.getModuleId();
        List<Long> ids = msgPush.getReceiveIdList();
        List<User> users = null;
        if(receiveType==MsgReceiveType.ALL.getCode()){
            users = usMapper.receiveAllUsers(moduleId, pushType);
        }else if(receiveType==MsgReceiveType.USER.getCode()){
            users = usMapper.receiveUsersByUserIds(moduleId, pushType, ids);
        }else if(receiveType==MsgReceiveType.DEPT.getCode()){
            users = usMapper.receiveUsersByDeptIds(moduleId, pushType, ids);
        }else if(receiveType==MsgReceiveType.GROUP.getCode()){
            users = usMapper.receiveUsersByGroupIds(moduleId, pushType, ids);
        }else if(receiveType==MsgReceiveType.POSITION.getCode()){
            users = usMapper.receiveUsersByPositionIds(moduleId, pushType, ids);
        }else if(receiveType==MsgReceiveType.ROLE.getCode()){
            users = usMapper.receiveUsersByRoleIds(moduleId, pushType, ids);
        }
        return users;
    }

    /**
     * 推送失败记录
     * @Title: pushError 
     * @Description: TODO
     * @param msg
     * @param user
     * @param pushType
     * @return: void
     */
    protected void pushError(MsgPush msgPush, User user,String pushType) {
        MsgPushLog mpl = new MsgPushLog();
        mpl.setPushNumber(msgPush.getPushNumber()+1);
        mpl.setPushTime(new Date());
        mpl.setReceiveType(MsgReceiveType.USER.getCode());
        mpl.setPushType(pushType);
        mpl.setReceiveId(user.getId());
        mpl.setReceiveName(user.getName());
        mpl.setModuleId(msgPush.getModuleId());
        mpl.setModuleName(msgPush.getModuleName());
        mpl.setMsgContent(msgPush.getMsgContent());
        mpl.setMsgTitle(msgPush.getMsgTitle());
        mpl.setSendUserId(msgPush.getSendUserId());
        mpl.setSendUserName(msgPush.getSendUserName());
        mpl.insert();
    }
    /**
     * 推送失败记录-全体
     * @Title: pushAllError 
     * @Description: TODO
     * @param msg
     * @param pushType
     * @return: void
     */
    protected void pushAllError(MsgPush msgPush, String pushType) {
        MsgPushLog mpl = new MsgPushLog();
        mpl.setPushNumber(mpl.getPushNumber()+1);
        mpl.setPushTime(new Date());
        mpl.setReceiveType(MsgReceiveType.ALL.getCode());
        mpl.setPushType(pushType);
        mpl.setModuleId(msgPush.getModuleId());
        mpl.setModuleName(msgPush.getModuleName());
        mpl.setMsgContent(msgPush.getMsgContent());
        mpl.setMsgTitle(msgPush.getMsgTitle());
        mpl.setSendUserId(msgPush.getSendUserId());
        mpl.setSendUserName(msgPush.getSendUserName());
        mpl.insert();
    }
}
